package com.web.cms.pan.util;

import com.blue.core.util.NumberUtil;
import com.blue.jdbc.Page;
import com.web.cms.pan.message.FolderRequest;
import com.web.cms.pan.message.FolderResponse;
import com.web.cms.pan.model.Folder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 目录转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
public class FolderConvertor
{
	private FolderConvertor()
	{
	}
	
	public static Folder toFolder(FolderRequest request)
	{
		if (request == null)
			return null;
		
		Folder folder = new Folder();
		folder.setId(request.getId());
		folder.setParentId(request.getParentId());
		folder.setUserId(request.getUserId());
		folder.setName(request.getName());
		folder.setIcon(request.getIcon());
		folder.setCreateTime(request.getCreateTime());
		folder.setDeleteTime(request.getDeleteTime());
		folder.setFiles(request.getFiles());
		folder.setFileSize(request.getFileSize());
		folder.setState(request.getState());
		return folder;
	}
	
	public static List<Folder> toFolder(List<FolderRequest> requestList)
	{
		List<Folder> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (FolderRequest request : requestList)
		{
			list.add(toFolder(request));
		}
		return list;
	}
	
	public static FolderResponse toResponse(Folder folder)
	{
		if (folder == null)
			return null;
		
		FolderResponse response = new FolderResponse();
		response.setId(folder.getId());
		response.setParentId(folder.getParentId());
		response.setUserId(folder.getUserId());
		response.setName(folder.getName());
		response.setIcon(folder.getIcon());
		response.setCreateTime(folder.getCreateTime());
		response.setDeleteTime(folder.getDeleteTime());
		response.setFiles(folder.getFiles());
		response.setFileSize(folder.getFileSize());
		response.setState(folder.getState());

		response.setFileSizeName(NumberUtil.byteFormat(folder.getFileSize()));
		return response;
	}
	
	public static List<FolderResponse> toResponse(List<Folder> list)
	{
		List<FolderResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Folder folder : list)
		{
			responseList.add(toResponse(folder));
		}
		return responseList;
	}
	
	public static Map<Integer, FolderResponse> toResponse(Map<Integer, Folder> map)
	{
		Map<Integer, FolderResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Folder> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}

	public static Map<String, List<FolderResponse>> toTreeMap(Map<Integer, List<Folder>> map)
	{
		Map<String, List<FolderResponse>> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;

		for (Map.Entry<Integer, List<Folder>> entry : map.entrySet())
		{
			responseMap.put(String.valueOf(entry.getKey()), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<FolderResponse> toResponse(Page<Folder> page)
	{
		if (page == null)
			return null;
		
		Page<FolderResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
